package com.xing.designmode;

import java.util.List;
import com.xing.controller.DBController;
import com.xing.model.HsdDataResult;
import com.xing.model.HsdModelAnswer;
import com.xing.model.HsdModelQuestion;
import com.xing.questionnairesurvey.R;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

/**
 * 每行单选题，每一行所对应的可供选答案是相同的。
 * 
 * @author Administrator
 * 
 */
public class SingleChoiceEachRow1 extends QuestionAnswer {

	private List<HsdModelQuestion> childHsdModelQuestionList;
	
	/**
	 * 一个用来记数的变量,用来统计是否每个问题都有答案了.
	 */
	 static int  flag = 0;
	/**
	 * 是否每个问题都有结果
	 */
	private boolean isHaveResult;
	
	private String lastQuestionuuid;
//	private boolean isHaveSelected;
	
	private Handler getParentQuestionHandler = new Handler() {
		public void handleMessage(Message msg) {
			HsdModelQuestion hsdModelQuestion = (HsdModelQuestion) msg.obj;
			/**
			 * 得到子问题
			 */
			DBController.getInstance().getChildHsdModelQuestionObjec(
					hsdModelQuestion.getSurveyuuid(),
					hsdModelQuestion.getQuestionuuid(), context,
					getChildQuestionHandler);
		}
	};

	private Handler getChildQuestionHandler = new Handler() {
		public void handleMessage(Message msg) {
			/**
			 * 子问题list
			 */
			childHsdModelQuestionList = (List<HsdModelQuestion>) msg.obj;

			/**
			 * 得到每一个子问题对应的可供选答案,每一个子问题的可供选答案都是一样的.只要得到其中一个问题的可供选答案即可.
			 */
			if (childHsdModelQuestionList != null
					&& childHsdModelQuestionList.size() > 0) {
				DBController.getInstance().getHsdModelAnswerList(
						childHsdModelQuestionList.get(0)
								.getFatherquestionuuid(),
								childHsdModelQuestionList.get(0).getSurveyuuid(), context,
						getAnswerHandler);
			}

		}
	};

	private Handler getAnswerHandler = new Handler() {
		public void handleMessage(Message msg) {
			final List<HsdModelAnswer> hsdModelAnswerList = (List<HsdModelAnswer>) msg.obj;
		
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			for (final HsdModelQuestion hsdModelQuestion : childHsdModelQuestionList) {
				LinearLayout linearLayout = (LinearLayout) inflater.inflate(
						R.layout.single_choice_item, null);
			    final RadioGroup radioGroup = (RadioGroup) linearLayout
						.findViewById(R.id.single_c_radio);
				TextView textView = (TextView) linearLayout
						.findViewById(R.id.single_c_tv);
				textView.setText(hsdModelQuestion.getDescription());
				
				/**
				 * 得到每一个子问题所对应的所选答案。
				 */
				DBController.getInstance().getHsdDataResultList(researchuuid,surveyuuid, hsdModelQuestion.getQuestionuuid(),context, new CompleteHandler(){
					void onComplete(Message msg) {
						final List<HsdDataResult> list = (List<HsdDataResult>) msg.obj;
						
						flag = flag + list.size();
						deal();
						/**
						 * 可供选答案
						 */
						for (final HsdModelAnswer hsdModelAnswer : hsdModelAnswerList) {
							
						    final RadioButton radioButton = new RadioButton(context);
//							radioButton.setBackgroundDrawable(getActivity().getResources().getDrawable(R.layout.radio_button_selector));
//							radioButton.setId(i);
							radioButton.setTag(hsdModelAnswer);
							radioButton.setText(hsdModelAnswer.getName());
							radioButton.setTextColor(Color.BLACK);
							radioGroup.addView(radioButton);
							
							
						/**
						 * 已有答案
						 */
						if (list != null && list.size() > 0) {
							for (HsdDataResult hsdDataResult : list) {
								System.out.println("可供选答案--->"+hsdModelAnswer.getAnsweruuid());
								if (hsdModelAnswer.getAnsweruuid().equals(hsdDataResult.getAnsweruuid())) {
				            		System.out.println("所选答案"+hsdModelAnswer.getAnsweruuid());
				            		radioButton.setChecked(true);
								}
							}
							
							/**
							 * 已有答案,进行更新操作
							 */
							radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//								
								@Override
								public void onCheckedChanged(RadioGroup group, int checkedId) {
									// TODO Auto-generated method stub
									System.out.println("hsdModelAnswer.getQuestionuuid()-------------"+hsdModelAnswer.getQuestionuuid());
								    int id = group.getCheckedRadioButtonId();
									RadioButton currentRadioButton = (RadioButton) view.findViewById(id);
									String answertext = currentRadioButton.getText().toString();
									HsdModelAnswer hsdModelAnswer = (HsdModelAnswer) currentRadioButton.getTag();
									
									DBController.getInstance().updateQuestionResult(researchuuid, surveyuuid, hsdModelAnswer.getAnsweruuid(), hsdModelQuestion.getQuestionuuid(), answertext, context, insertQuestionResultHandler);
								}
							});
							
							
						}else {
							/**
							 * 还没有答案,进行插入操作
							 */
							radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//								
								@Override
								public void onCheckedChanged(RadioGroup group, int checkedId) {
									// TODO Auto-generated method stub
									
								    int id = group.getCheckedRadioButtonId();
									RadioButton currentRadioButton = (RadioButton) view.findViewById(id);
									final String answertext = currentRadioButton.getText().toString();
									final HsdModelAnswer hsdModelAnswer = (HsdModelAnswer) currentRadioButton.getTag();
									
									DBController.getInstance().getHsdDataResultList(researchuuid,surveyuuid, hsdModelQuestion.getQuestionuuid(),context, new CompleteHandler(){
										void onComplete(Message msg) {
											List<HsdDataResult> currentHsdDataResultlist = (List<HsdDataResult>) msg.obj;
											if (currentHsdDataResultlist != null && currentHsdDataResultlist.size() > 0) {
												DBController.getInstance().updateQuestionResult(researchuuid, surveyuuid, hsdModelAnswer.getAnsweruuid(), hsdModelQuestion.getQuestionuuid(), answertext, context, insertQuestionResultHandler);
											}else {
												flag = flag + 1;
												deal();
												DBController.getInstance().insertQuestionResult(researchuuid, surveyuuid, hsdModelAnswer.getAnsweruuid(),hsdModelQuestion.getQuestionuuid(),answertext, context,insertQuestionResultHandler);
											}
										}});
								}
							});
						}
						}
						
						
					}
					
				});
				
				answerParentLayout.addView(linearLayout);
			}

		}
	};
	
	private void deal(){
		if (flag == childHsdModelQuestionList.size()) {
			isHaveResult = true;
		}else {
			isHaveResult = false;
		}
	}
	
	private Handler insertQuestionResultHandler = new Handler(){
		public void handleMessage(Message msg) {
			
		}
	};

	@Override
	public void showAnswer() {

	}

	public void showAnswer(HsdModelQuestion hsdModelQuestion) {
		/**
		 * 得到父问题
		 */
		DBController.getInstance().getHsdModelQuestionObject(
				hsdModelQuestion.getSurveyuuid(),
				String.valueOf(hsdModelQuestion.getSortindex()), context,
				getParentQuestionHandler);
	}

	/**
	 * 
	 */
	class CompleteHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			onComplete(msg);
			
		}
		
		void onComplete(Message msg){
			
		}
	}
	
	@Override
	public boolean isHasResult() {
		// TODO Auto-generated method stub
		return isHaveResult;
	}
}
